/**
 * 
 */
package qmese.matrix;


/**
 * @author Pawel
 * 
 */
public class MatrixUtils {

	public static String print(Matrix A) {
		String text = "[";
		int[][] dataA = A.getData();

		for (int i = 0; i < A.getM(); i++) {
			for (int j = 0; j < A.getN(); j++) {
				text += dataA[i][j] + ", ";
			}
			text = text.substring(0, text.length() - 2);
			text += "| ";
		}
		text = text.substring(0, text.length() - 2);
		text += "];";

		return text;
	}

	public static Matrix create(String data) {
		try {
			int M = 0;
			int N = 0;
			for (int i = 0; i < data.length(); i++) {
				if (data.toCharArray()[i] == ('|')) {
					M++;
				}
				if (data.toCharArray()[i] == (',')) {
					N++;
				}
			}
			M++;
			N = N + M;
			N = N / M;
			
			String temp = data.replaceAll("[", "");
			temp = temp.replaceAll("|", ",");
			temp = temp.replaceAll("]", "");
			temp = temp.replaceAll(" ", "");
			
			String[] listOfValues = temp.split(",");
			int[] values = new int[listOfValues.length];
			for (int i = 0; i < listOfValues.length; i++) {
				values[i] = Integer.parseInt(listOfValues[i]);
			}
			
			int counter = 0;
			int[][] matrixData = new int[M][N];
			for (int i = 0; i< matrixData.length; i++) {
				for (int j = 0; j < matrixData[0].length; j++) {
					matrixData[i][j] = values[counter];
					counter++;
				}
			}
			
			return null;
		} catch (Exception e) {
			System.out.println(e);
			return null;
		}
	}

	public static Matrix create(int[][] data) {
		Matrix A = new Matrix(data);
		return A;
	}

	public static Matrix create(int M, int N) {
		Matrix A = new Matrix(M, N);
		return A;
	}

	public static Matrix add(Matrix A, Matrix B) {
		Matrix C = new Matrix(A.getM(), A.getN());
		int[][] dataC = C.getData();

		int[][] dataA = A.getData();
		int[][] dataB = B.getData();

		for (int i = 0; i < A.getM(); i++) {
			for (int j = 0; j < A.getN(); j++) {
				dataC[i][j] = dataA[i][j] + dataB[i][j];
			}
		}

		return C;
	}

	public static Matrix sub(Matrix A, Matrix B) {
		Matrix C = new Matrix(A.getM(), A.getN());
		int[][] dataC = C.getData();

		int[][] dataA = A.getData();
		int[][] dataB = B.getData();

		for (int i = 0; i < A.getM(); i++) {
			for (int j = 0; j < A.getN(); j++) {
				dataC[i][j] = dataA[i][j] - dataB[i][j];
			}
		}

		return C;
	}

	public static Matrix multiple(Matrix A, Matrix B) {
		Matrix C = new Matrix(A.getM(), A.getN());
		int[][] dataC = C.getData();

		int[][] dataA = A.getData();
		int[][] dataB = B.getData();

		for (int i = 0; i < A.getM(); i++) {
			for (int j = 0; j < A.getN(); j++) {
				dataC[i][j] = dataA[i][j] * dataB[i][j];
			}
		}

		return C;
	}
}
